package com.css.fxfzaqbz.modules.data.controller;

import com.css.fxfzaqbz.base.response.RestResponse;
import com.css.fxfzaqbz.modules.data.entity.SchoolData;
import com.css.fxfzaqbz.modules.data.queryParams.SchoolDataQueryParams;
import com.css.fxfzaqbz.modules.data.service.SchoolDataService;
import com.css.fxfzaqbz.util.FileUtil2;
import com.css.fxfzaqbz.util.PlatformPageUtils;
import com.css.fxfzaqbz.web.PlatformSessionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 3.3.4学校医院数据管理子模块 -学校数据表单
 */
@RestController
@RequestMapping("/fxfzaqbz/data")
@Slf4j
public class SchoolDataController {
    @Resource
    SchoolDataService schoolDataService ;

    /**
     * 新增schoolData
     * @param schoolData
     * @return
     */
    @PostMapping("/saveSchoolData")
    public RestResponse saveSchoolData (@RequestBody SchoolData schoolData){
        RestResponse restResponse = null ;
        try{
            String userID = PlatformSessionContext.getUserID();
            schoolData.setCreateUser(userID);
            restResponse = RestResponse.succeed(schoolDataService.saveSchoolData(schoolData));
        }catch (Exception e){
            log.error("新增学校数据失败",e);
            restResponse = RestResponse.fail("新增学校数据失败!");
        }
        return restResponse;
    }

    /**
     * 全查询  模糊查询未设定条件
     * @param request
     * @param queryParams
     * @return
     */
    @GetMapping("/getAllSchoolData")
    public RestResponse getAllSchoolData(HttpServletRequest request, SchoolDataQueryParams queryParams){

        RestResponse restResponse = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            queryParams.setCurPage(curPage);
            queryParams.setPageSize(Integer.parseInt(PlatformPageUtils.PAGE_SIZE_DEF));
            restResponse = RestResponse.succeed(pageSize);
        }catch (Exception e){
            log.error("查询学校数据失败!",e);
            restResponse = RestResponse.fail("查询学校数据失败");
        }
        return restResponse;
    }

    /**
     * 根据id查询SchoolData数据
     * @param id
     * @return
     */
    @GetMapping("/getSchoolData/{id}")
    public RestResponse<SchoolData> getUser(@PathVariable("id") String id) {
        RestResponse restResponse = null;
        try{
            restResponse = RestResponse.succeed(schoolDataService.getSchoolDataById(id));
        }catch (Exception e){
            log.error("获取学校数据失败!",e);
            restResponse = RestResponse.fail("获取学校数据失败!");
        }
        return restResponse;
    }


    /**
     * 逻辑删除，修改数据标志位
     * @param ids
     * @return
     */
    @PostMapping("/delSchoolData/{ids}")
    public RestResponse deleteSchoolData(@PathVariable("ids") List<String> ids) {
        RestResponse restResponse = null;
        try{
            String userID = PlatformSessionContext.getUserID();
            schoolDataService.deleteSchoolDatas(ids,userID);
            restResponse = RestResponse.succeed();
        }catch (Exception e){
            log.error("删除学校数据失败!",e);
            restResponse = RestResponse.fail("删除学校数据失败!");
        }
        return restResponse;
    }

    /**
     * 修改
     * @param schoolData
     * @return
     */
//    @PostMapping("/updateSchoolData")
//    public RestResponse<SchoolData> updateSchoolData(@RequestBody SchoolData schoolData)  {
//        RestResponse restResponse = null;
//        try{
//            restResponse = RestResponse.succeed(schoolDataService.updateSchoolData(schoolData));
//        }catch (Exception e){
//            log.error("修改学校数据失败!",e);
//            restResponse = RestResponse.fail("修改学校数据失败!");
//        }
//        return restResponse;
//    }

    /**
     * 导出表内数据
     * @param response
     * @throws IOException
     */
    @PostMapping("/exportExcelSchoolData")
    public void exportExcel(HttpServletResponse response ) throws IOException {
        RestResponse restResponse = null;
        try{
            List<SchoolData> list  = schoolDataService.getAll();
            FileUtil2.exportExcel(list, "学校数据", "sheet", SchoolData.class, "SchoolData", response);
            restResponse = RestResponse.succeed("导出成功");
        }catch (Exception e){
            log.error("导出失败",e);
            restResponse = RestResponse.fail("导出失败");
        }
    }

    /**
     * 导入数据
     * @param file
     * @throws IOException
     */
    @PostMapping("/importExcelSchoolData")
    public RestResponse importExcel(MultipartFile file) throws IOException {
        RestResponse restResponse = null;
        try{
            schoolDataService.importExcel(file);
            restResponse = RestResponse.succeed("导入成功");
        }catch (Exception e){
            log.error("导入失败",e);

        }
        return restResponse ;
    }
}
